package com.coding.service.impl;

import com.coding.base.pojo.BaseModel;
import com.coding.base.pojo.PageQueryModel;
import com.coding.base.util.DateUtil;
import com.coding.dao.*;
import com.coding.pojo.*;
import com.coding.service.CollectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther wangchenghao
 * @date 2020/7/26
 **/
@Service
public class CollectImpl implements CollectService {
    @Autowired
    CollectDao collectDao;
    @Autowired
    PostDao postDao;
    @Autowired
    MainPlaceDao mainPlaceDao;
    @Autowired
    UserDao userDao;
    @Autowired
    PoemDao poemDao;
    @Autowired
    ThumbDao thumbDao;
    @Autowired
    CommentDao commentDao;

    @Override
    public void checkCollect(int user_id, int source_id, String source_kind, BaseModel baseModel) throws Exception {
        Collect collect=collectDao.selectAll(user_id,source_id,source_kind);
        if (collect!=null){
            baseModel.setSucceed("用户已收藏",1);
        }else {
            baseModel.setFailure("用户没有收藏",0);
        }
    }

    @Override
    public void delete(int user_id, int source_id, String source_kind, BaseModel baseModel) throws Exception {
        int i = collectDao.deleteCollect(user_id, source_id, source_kind);
        if (i == 1) {
            baseModel.setSucceed("成功取消收藏", i);
        } else {
            baseModel.setFailure("取消收藏失败");
        }
    }

    @Override
    public void addnewcollectpost(int user_id, int source_id, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        Post post = postDao.selectByPrimaryKey(source_id);
        if (post.getPost_title() == null) {
            baseModel.setFailure("查无此帖子资源");
            return;
        }
        Collect lastCollect = collectDao.selectAll(user_id, source_id, "post");
        if (lastCollect != null) {
            baseModel.setFailure("已添加收藏");
            return;
        }
        Collect collect = new Collect();
        collect.setUser_id(user_id);
        collect.setSource_id(source_id);
        collect.setCollect_kind("post");
        collect.setCollect_time(DateUtil.getStringDate());
        MainPlace mainPlace = mainPlaceDao.selectByPrimaryKey(post.getPost_place_id());
        switch (mainPlace.getMain_way_id()) {
            case 1:
                collect.setCollect_more1("浙东");
                break;
            case 2:
                collect.setCollect_more1("大运河");
                break;
            case 3:
                collect.setCollect_more1("钱塘");
                break;
            case 4:
                collect.setCollect_more1("瓯江");
                break;
            default:
                break;
        }
        Post newPost = new Post();
        newPost=postDao.selectByPrimaryKey(source_id);
        if (collectDao.insertSelective(collect) == 1) {
            int collectNumbers = collectDao.countCollectTimes(source_id, "post");
            if (collectNumbers > 0) {
                newPost.setPost_collect(collectNumbers);
            } else {
                newPost.setPost_collect(0);
            }
            int thumbNumbers = thumbDao.countThumb(source_id, "post");
            if (thumbNumbers > 0) {
                newPost.setPost_likes(thumbNumbers);
            } else {
                newPost.setPost_likes(0);
            }
            int commentNumbers = commentDao.countComments(source_id, "post");
            if (commentNumbers > 0) {
                newPost.setPost_comments(commentNumbers);
            } else {
                newPost.setPost_comments(0);
            }
            Collect thecollect = collectDao.selectAll(user_id, newPost.getPost_id(), "post");
            if (thecollect != null) {
                newPost.setPost_collect_status(1);
            }
            Thumb thumb = thumbDao.selectAll(user_id, newPost.getPost_id(), "post");
            if (thumb != null) {
                newPost.setPost_likes_status(1);
            }
            OssImages ossImages=newPost.getOssImages();
            Usercollectpost usercollectpost=new Usercollectpost();
            usercollectpost.setCollect(collect);
            usercollectpost.setPost(newPost);
            usercollectpost.setOssImages(ossImages);
            baseModel.setSucceed("成功添加帖子收藏", usercollectpost);
        } else {
            baseModel.setFailure("添加帖子收藏失败");
        }

    }

    @Override
    public void getcollectpost(int user_id, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        PageHelper.startPage(pageQueryModel.getPage(), pageQueryModel.getSize());
        List<Usercollectpost> usercollectposts = collectDao.selectpost(user_id);
        PageInfo pageInfo = new PageInfo(usercollectposts);
        usercollectposts = pageInfo.getList();
        if (usercollectposts.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                baseModel.setFailure("已经没有下一页了");
            } else {
                baseModel.setOther(2, "没有符合条件的收藏");
            }
            return;
        }
        baseModel.setSucceed("查询成功，符合条件的收藏共" + usercollectposts.size() + "条", usercollectposts);
    }

    @Override
    public void addnewcollectplace(int user_id, int source_id, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        MainPlace mainPlace = mainPlaceDao.selectByPrimaryKey(source_id);
        if (mainPlace == null) {
            baseModel.setFailure("查无此景点资源");
            return;
        }
        Collect lastCollect = collectDao.selectAll(user_id, source_id, "mainplace");
        if (lastCollect != null) {
            baseModel.setFailure("已添加收藏");
            return;
        }
        Collect collect = new Collect();
        collect.setUser_id(user_id);
        collect.setSource_id(source_id);
        collect.setCollect_kind("mainplace");
        collect.setCollect_time(DateUtil.getStringDate());
        switch (mainPlace.getMain_way_id()) {
            case 1:
                collect.setCollect_more1("浙东");
                break;
            case 2:
                collect.setCollect_more1("大运河");
                break;
            case 3:
                collect.setCollect_more1("钱塘");
                break;
            case 4:
                collect.setCollect_more1("瓯江");
                break;
            default:
                break;
        }

        if (collectDao.insertSelective(collect) == 1) {
            baseModel.setSucceed("成功添加帖子收藏", collect);
        } else {
            baseModel.setFailure("添加帖子收藏失败");
        }
    }

    @Override
    public void getcollectplace(int user_id, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        PageHelper.startPage(pageQueryModel.getPage(), pageQueryModel.getSize());
        List<Usercollectplace> usercollectplaceList = collectDao.selectMainplace(user_id);
        PageInfo pageInfo = new PageInfo(usercollectplaceList);
        usercollectplaceList = pageInfo.getList();
        if (usercollectplaceList.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                baseModel.setFailure("已经没有下一页了");
            } else {
                baseModel.setOther(2, "没有符合条件的收藏");
            }
            return;
        }
        baseModel.setSucceed("查询成功，符合条件的收藏共" + usercollectplaceList.size() + "条", usercollectplaceList);
    }

    @Override
    public void addNewCollectPoem(int user_id, int source_id, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        Poem poem = poemDao.selectByPrimaryKey(source_id);
        if (poem == null) {
            baseModel.setFailure("查无此诗词资源");
            return;
        }
        Collect lastCollect = collectDao.selectAll(user_id, source_id, "poem");
        if (lastCollect != null) {
            baseModel.setFailure("已添加收藏");
            return;
        }
        Collect collect = new Collect();
        collect.setUser_id(user_id);
        collect.setSource_id(source_id);
        collect.setCollect_kind("poem");
        collect.setCollect_time(DateUtil.getStringDate());
        switch (poem.getPoem_place()) {
            case 1:
                collect.setCollect_more1("浙东");
                break;
            case 2:
                collect.setCollect_more1("大运河");
                break;
            case 3:
                collect.setCollect_more1("钱塘江");
                break;
            case 4:
                collect.setCollect_more1("瓯江");
                break;
            default:
                break;
        }
        if (collectDao.insertSelective(collect) == 1) {
            baseModel.setSucceed("成功添加诗词收藏", collect);
        } else {
            baseModel.setFailure("添加诗词收藏失败");
        }
    }

    @Override
    public void getCollectPoem(int user_id, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        PageHelper.startPage(pageQueryModel.getPage(), pageQueryModel.getSize());
        List<Usercollectpoem> usercollectpoemList = collectDao.selectpoem(user_id);
        PageInfo pageInfo = new PageInfo(usercollectpoemList);
        usercollectpoemList = pageInfo.getList();
        if (usercollectpoemList.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                baseModel.setFailure("已经没有下一页了");
            } else {
                baseModel.setOther(2, "没有符合条件的收藏");
            }
            return;
        }
        baseModel.setSucceed("查询成功，符合条件的收藏共" + usercollectpoemList.size() + "条", usercollectpoemList);
    }

    @Override
    public void getCollectAll(int user_id, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        UsercollectAll usercollectAll = new UsercollectAll();
        List<Usercollectpost> usercollectpostList = collectDao.selectpost(user_id);
        List<Usercollectplace> usercollectplaceList = collectDao.selectMainplace(user_id);
        List<Usercollectpoem> usercollectpoemList = collectDao.selectpoem(user_id);
        if (usercollectpostList != null) {
            usercollectAll.setUsercollectpostList(usercollectpostList);
        }
        if (usercollectplaceList != null) {
            usercollectAll.setUsercollectplaceList(usercollectplaceList);
        }
        if (usercollectpoemList != null) {
            usercollectAll.setUsercollectpoemList(usercollectpoemList);
        }
        if (usercollectAll != null) {
            baseModel.setSucceed("成功获取用户历史记录", usercollectAll);
        } else {
            baseModel.setFailure("获取用户历史记录失败");
        }
    }

    @Override
    public void getUserCollect(int user_id, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        List<UserCollect> userCollectList = new ArrayList<>();
        List<Collect> collectList = collectDao.getByTime(user_id);
        if (collectList == null) {
            baseModel.setFailure("该用户暂无收藏");
        }
        int i = 1;
        for (Collect collect : collectList
        ) {
            if (collect.getCollect_kind().equals("mainplace")) {
                Usercollectplace usercollectplace = collectDao.selectMainplaceSingle(collect.getSource_id());
                UserCollect userCollect = new UserCollect(i, user_id, user.getUser_name(), collect.getSource_id(), collect.getCollect_kind(), collect.getCollect_time(), usercollectplace.getOssImages().getImage_url(), collect.getCollect_more1());
                userCollect.setMainPlace(usercollectplace.getMainPlace());
                userCollectList.add(userCollect);
                i++;
                continue;
            }
            if (collect.getCollect_kind().equals("post")) {
                Usercollectpost usercollectpost = collectDao.selectPostSingle(collect.getSource_id());
                UserCollect userCollect = new UserCollect(i, user_id, user.getUser_name(), collect.getSource_id(), collect.getCollect_kind(), collect.getCollect_time(), usercollectpost.getOssImages().getImage_url(), collect.getCollect_more1());
                userCollect.setPost(usercollectpost.getPost());
                userCollectList.add(userCollect);
                i++;
                continue;
            }
            if (collect.getCollect_kind().equals("poem")) {
                Usercollectpoem usercollectpoem = collectDao.selectPoemSingle(collect.getSource_id());
                UserCollect userCollect = new UserCollect(i, user_id, user.getUser_name(), collect.getSource_id(), collect.getCollect_kind(), collect.getCollect_time(), usercollectpoem.getOssImages().getImage_url(), collect.getCollect_more1());
                userCollect.setPoem(usercollectpoem.getPoem());
                userCollectList.add(userCollect);
                i++;
                continue;
            }
        }
        baseModel.setSucceed("成功获得用户收藏记录共" + (i - 1) + "条", userCollectList);
    }

    @Override
    public void deleteCollect(int user_id, int source_id, String source_kind, BaseModel baseModel) throws Exception {
        if (collectDao.deleteCollect(user_id, source_id, source_kind) == 1) {
            if (source_kind.equals("post")) {
                Post newPost = new Post();
                newPost=postDao.selectByPrimaryKey(source_id);
                int collectNumbers = collectDao.countCollectTimes(source_id, "post");
                if (collectNumbers > 0) {
                    newPost.setPost_collect(collectNumbers);
                } else {
                    newPost.setPost_collect(0);
                }
                int thumbNumbers = thumbDao.countThumb(source_id, "post");
                if (thumbNumbers > 0) {
                    newPost.setPost_likes(thumbNumbers);
                } else {
                    newPost.setPost_likes(0);
                }
                int commentNumbers = commentDao.countComments(source_id, "post");
                if (commentNumbers > 0) {
                    newPost.setPost_comments(commentNumbers);
                } else {
                    newPost.setPost_comments(0);
                }
                Collect thecollect = collectDao.selectAll(user_id, newPost.getPost_id(), "post");
                if (thecollect != null) {
                    newPost.setPost_collect_status(1);
                }
                Thumb thumb = thumbDao.selectAll(user_id, newPost.getPost_id(), "post");
                if (thumb != null) {
                    newPost.setPost_likes_status(1);
                }
                baseModel.setSucceed("成功删除帖子收藏",newPost);
                return;
            }
            baseModel.setSucceed("删除收藏成功");
        } else {
            baseModel.setFailure("删除收藏失败");
        }
    }


}
